﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using APIShoppingSystem.Models.MenuInfoViewModel;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using ShoppingEntity.ResultModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MenuInfoController : ControllerBase
    {
        private IMenuInfoBll _menuInfoBll;
        public MenuInfoController(IMenuInfoBll menuInfoBll)
        {
            _menuInfoBll = menuInfoBll;
        }


        /// <summary>
        /// 获取首页菜单集合
        /// </summary>
        /// <returns></returns>
        
        [HttpGet]
        public IActionResult GetInitMenus()
        {
            var res = new GetInitMenuResultModel();
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo != null)
            {
                //获取菜单集合信息
                List<MenuInfoResultModel> menus = _menuInfoBll.GetInitMenus(userInfo);
                res.SetMenuData(menus);
            }

            return new JsonResult(res);
        }



        /// <summary>
        /// 获取菜单集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuInfoList(int page, int limit, string title, string describe)
        {
            int count;
            object menuInfo = _menuInfoBll.GetMenuInfoByPage(page, limit, out count, title, describe);
            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = menuInfo
            });
        }


        /// <summary>
        /// 菜单添加数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenuInfo([FromForm]AddMenuInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (string.IsNullOrEmpty(model.Title))
            {
                res.Msg = "标题不能为空";
                return new JsonResult(res);
            }
            MenuInfo entity = new MenuInfo()
            {
                ID = Guid.NewGuid().ToString(),
                Title = model.Title,
                Describe = model.Describe,
                Level = model.Level,
                Sort = model.Sort,
                Href = model.Href,
                ParentId = model.ParentId,
                Icon = model.Icon,
                Target = model.Target,
                CreateTime = DateTime.Now,
                IsDelete = false
            };
            bool IsSuccess = _menuInfoBll.Add(entity);
            if (IsSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "添加成功";
                res.Status = 1;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 查询菜单列表数据
        /// </summary>
        /// <param name="menuInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUpdateMenuInfo(string menuInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取菜单信息
            MenuInfo menuInfo = _menuInfoBll.GetEntity(menuInfoId);
            //获取菜单下拉框选数据
            object options = _menuInfoBll.GetMenuInfoSelectOption(menuInfoId);
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";

            res.Datas = new
            {
                MenuInfo = menuInfo,
                Options = options
            };
            return new JsonResult(res);
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateMenuInfo([FromForm] UpdateMenuInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_menuInfoBll.UpdateMenuInfo(model.ID, model.Title, model.Describe, model.Level, model.Sort, model.Href, model.ParentId, model.Icon, model.Target))
            {
                res.IsSuccess = true;
                res.Msg = "修改成功";
                res.Status = 1;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 软删除单条数据
        /// </summary>
        [HttpPost]
        public IActionResult DeleteMenuInfo([FromForm] string menunfoId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_menuInfoBll.SoftDeleteEntity(menunfoId))
            {
                res.Status = 1;
                res.Msg = "删除成功";
                res.IsSuccess = true;
                return new JsonResult(res);
            }
            else
            {
                res.Msg = "删除失败";
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 软删除多个数据
        /// </summary>
        /// <param name="menuInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteMenuInfos([FromForm] List<string> menuInfoIds)
        {
            CustomActionResult res = new CustomActionResult();

            //判断是否为管理员
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                return new JsonResult(res);
            }
            else
            {
                if (!userInfo.IsAdmin)
                {
                    res.Msg = "当前用户没有权限操作";
                    return new JsonResult(res);
                }
            }

            if (menuInfoIds == null || menuInfoIds.Count == 0)
            {
                res.Msg = "请选择要删除的数据";
            }
            else
            {
                _menuInfoBll.SoftDeleteEntities(menuInfoIds);
                res.Status = 1;
                res.Msg = "删除成功";
                res.IsSuccess = true;
            }
            return new JsonResult(res);
        }



        /// <summary>
        /// 获取菜单下拉接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuInfoSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            //获取菜单下拉选数据
            object options = _menuInfoBll.GetMenuInfoSelectOption();

            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Datas = options;
            return new JsonResult(res);
        }
    }
}
